package com.kotlin.flong.coroutines1

import kotlinx.coroutines.*

/**
 * @Author:liangjl
 * @Date: 2020/7/9-19:52
 * @Eamil:jilongliang@sina.com
 * @Description:
 * CoroutineScope在组件生命周期的重要性
 * 1、Dispatchers.Main 需要添加'kotlinx-coroutines-android' 依赖
 * 2、by 表示委托关键字
 */

/*
var by = CoroutineScope(Dispatchers.Default)
class Activity : CoroutineScope by by{}
*/

class Activity : CoroutineScope by CoroutineScope(Dispatchers.Default) {
    fun destroy() {
        //取消协程，在CoroutineScope下的协程都会取消
        cancel()
    }

    fun doSomeThing() {
        repeat(8) { i ->
            launch {
                delay((i + 1) * 300L)
                println("Coroutine $i is finished")
            }
        }
    }
}

fun main() = runBlocking<Unit> {

    var activity = Activity()
    activity.doSomeThing()

    println("启动协程")
    delay(1300L)

    println("销毁activity协程")
    activity.destroy()

    delay(5000L)


}